//截取URL参数
// export const getQuery = (name, url) => {
//   var u = arguments[1] || window.location.search,
//     reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)'),
//     r = u.substr(u.indexOf('\?') + 1).match(reg);
//   return r != null ? r[2] : '';
// }

//检查邮箱
export const checkEmail = email => {
  return /^([a-zA-Z0-9-_]*[-_.]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[.][a-zA-Z]{2,3}([.][a-zA-Z]{2})?$/.test(email);
}

//检查手机
export const checkMobile = mobile => {
  return /^1\d{10}$/.test(mobile);
}

// 将秒转换为时分秒格式
export const formatSeconds = seconds => {
  return [
    parseInt(seconds / 3600),
    parseInt(seconds / 60 % 60),
    parseInt(seconds % 60)
  ]
    .join(":")
    .replace(/\b(\d)\b/g, "0$1");
}

// 将金额格式化为三位跟一个逗号的格式
export const formatMoney = (s) => {
  /*
  *  s: 金额， l：反转后的数字数组， r：小数点， t：最终数值
  * */
  s = parseFloat((s + "").replace(/[^\d\.-]/g, "")) + "";
  let l = s.split(".")[0].split("").reverse(),
    r = s.split(".")[1] || '',
    t = "";
  for (let i = 0; i < l.length; i++) {
    t += l[i] + ((i + 1) % 3 === 0 && (i + 1) !== l.length ? "," : "");
  }
  if (r) {
    return t.split("").reverse().join("") + "." + r;
  } else {
    return t.split("").reverse().join("");
  }

}

// 时间格式转换
export const formatDate = (time = Date.now(), fmt = 'yyyy-MM-dd hh:mm:ss') => {
  let date = new Date(time);
  let o = {
    'M+': date.getMonth() + 1, //月份
    'd+': date.getDate(), //日
    'h+': date.getHours(), //小时
    'm+': date.getMinutes(), //分
    's+': date.getSeconds(), //秒
    'q+': Math.floor((date.getMonth() + 3) / 3), //季度
    'S': date.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
    }
  }
  return fmt;
}

// 获取某年某月有多少天
export const getMonthDays = (month, year = new Date().getFullYear()) => {
  let dayCount,
    now = new Date(year, month, 0);
  dayCount = now.getDate();
  return dayCount;
}

//获取中文星期
export const getDay = (time = Date.now(), prefix = '周') => {
  let str = '日一二三四五六';
  return prefix + str[new Date(time).getDay()];
}

// 输入合法数字，输出周几
export const outWeekDay = (n) => {
  if (n === 0) {
    return;
  }
  let prefix = "周",
    weekly = "";
  switch (n) {
    case 1:
      weekly = "一";
      break;
    case 2:
      weekly = "二";
      break;
    case 3:
      weekly = "三";
      break;
    case 4:
      weekly = "四";
      break;
    case 5:
      weekly = "五";
      break;
    case 6:
      weekly = "六";
      break;
    case 7:
      weekly = "日";
      break;
    default:
      weekly = ""
  }
  return prefix + weekly;
}

//  当前数值占总数的百分比
export const proportion = (current, total, fixed = 2) => {
  if (total === 0) {
    return 0
  }
  return parseFloat(((current / total) * 100).toFixed(fixed));
}

